package com.xinqi.modules.file.course.common;

import com.alibaba.fastjson.JSONObject;
import com.xinqi.common.rocketmq.util.ByteUtils;
import com.xinqi.common.third.oss.slice.dto.UserDataDTO;
import com.xinqi.common.third.oss.slice.event.OssSliceEvent;
import com.xinqi.modules.file.course.courseware.service.CourseWareSliceService;
import com.xinqi.modules.file.course.homework.service.HomeworkService;
import com.xinqi.modules.file.course.prepare.service.PrepareService;
import com.xinqi.modules.file.req.ContentDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.ByteBuffer;
import java.util.List;
import java.util.Objects;

import static com.xinqi.common.core.enums.RocketMqSliceEnum.*;

@Service
@RequiredArgsConstructor
@Log4j2
public class CommonService {

    private final HomeworkService homeworkService;

    private final CourseWareSliceService wareSliceService;

    private final PrepareService prepareService;

    private final OssSliceEvent ossSliceEvent;

    /**
     * 公共切片切入方法
     *
     * @author dzy
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean commonAliSlice(List<MessageExt> msgs) {
        for (MessageExt messageExt : msgs) {
            String body = ByteUtils.byteBufferToStr(ByteBuffer.wrap(messageExt.getBody()).asReadOnlyBuffer());
            log.info("body => {}", body);
            JSONObject jsonObject = JSONObject.parseObject(body);
            log.info("jsonObject => {}", jsonObject);

            Object content = jsonObject.get("content");
            log.info("content => {}", content);

            ContentDTO message = JSONObject.parseObject(content.toString(),ContentDTO.class);
            if(Objects.isNull(message)){
                return true;
            }
            log.info("message => {}", message);
            if (Objects.equals("Succeeded", message.getStatus())) {
                // 解析数据
                UserDataDTO userData = JSONObject.parseObject(message.getUserData(),UserDataDTO.class);
                log.info("userData => {}", userData);
                if (Objects.isNull(userData.getFolder())) {
                    return true;
                }
                List<String> list = ossSliceEvent.getSliceImage(userData.getFolder(), userData.getSuffix());
                log.info("list => {}", list);
                if (Objects.equals(HOMEWORK_SLICE.getCode(), userData.getType())) {
                    return homeworkService.sliceOssFile(userData.getKey(), list);
                } else if (Objects.equals(COURSEWARE_SLICE.getCode(), userData.getType())) {
                    return wareSliceService.sliceOssFile(userData.getKey(), list);
                } else if (Objects.equals(PREPARE_SLICE.getCode(), userData.getType())) {
                    return prepareService.sliceOssFile(userData.getKey(), list);
                }
            }
        }
        return false;
    }

}
